package org.iabako.client.ui.form.extranet.signup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.InputText;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.enumeration.IabakoPackage;
import org.iabako.shared.entity.enumeration.SignUpStep;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.user.SignUpServiceAsync;
import org.iabako.shared.tools.GenericTools;

/**
 * Created by jose on 10/12/14.
 */
public class SignUpEmailValidationForm extends Composite {

    interface MyUiBinder extends UiBinder<HTMLPanel, SignUpEmailValidationForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final SignUpServiceAsync signUpServiceAsync = SignUpServiceAsync.Util.getInstance();
    @UiField
    LabelText goBack;
    @UiField
    Button goAhead;
    @UiField
    LabelText activationHelpLabel;
    @UiField
    LabelText activationText;
    @UiField
    InputText keyCode;

    SignUpForm signUpForm;
    private int attempts;

    public SignUpEmailValidationForm() {
        initWidget(binderForm.createAndBindUi(this));

        goAhead.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                validate();
            }
        });

        goBack.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ValidationMessage.getFirstFromDOM().clear();
                signUpForm.goTo(SignUpStep.signUpUser);
            }
        });

        activationHelpLabel.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                String emailProvider = signUpForm.getUser().getEmail().substring(signUpForm.getUser().getEmail().indexOf("@") + 1);
                String text = Messages.getLabel("sign_up_help_no_validation_email_text", signUpForm.getUser().getEmail(), emailProvider);

                IabakoDialogBox iabakoDialogBox = new IabakoDialogBox("sign_up_help_no_validation_email_title", text);
                iabakoDialogBox.setAvoidClosePopupCall(true);

                NavigationController.getInstance().show(iabakoDialogBox);
            }
        });
    }

    public void setSignUpForm(SignUpForm signUpForm) {
        this.signUpForm = signUpForm;
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            activationText.setHtmlLabel(Messages.getLabel("sign_up_form_activation_text", signUpForm.getUser().getEmail()));
            activationHelpLabel.setHtmlLabel(Messages.getLabel("sign_up_form_activation_help", signUpForm.getUser().getEmail()));
        }

        super.setVisible(visible);
    }

    private void validate() {

        ValidationMessage.getFirstFromDOM().clear();

        if (GenericTools.isEmpty(keyCode.getValue())) {
            keyCode.addErrorMessage(keyCode, false, "validation_form_mandatory_red_fields_error");
            return;
        }

        signUpServiceAsync.keyCodeVerification(signUpForm.getUser(), keyCode.getValue(), signUpForm.getSignUpIabakoPackage(), new IabakoAsyncCallback<User>() {

            @Override
            public void onSuccess(User user) {
                attempts++;
                signUpForm.setUser(user);

                if (user.isActive() && signUpForm.getSignUpIabakoPackage().isFree()) {

                    signUpForm.goTo(SignUpStep.signUpDone);

                } else if (user.getUserPayPalToken() != null && signUpForm.getSignUpIabakoPackage() != IabakoPackage.free) {

                    signUpForm.goTo(SignUpStep.signUpPayment);

                } else if (attempts < 3) {

                    keyCode.addErrorMessage(keyCode, false, "validation_sign_up_key_code_error");

                } else {
                    NavigationController.getInstance().closePopups();
                    NavigationController.getInstance().show(new IabakoDialogBox("sign_up_attempts_limit_error_title", "sign_up_attempts_limit_error_message"));
                }
                super.onSuccess(user);
            }
        });
    }
}
